#include "private_implementations/JsonValue.h"

static const std::wstring json_true = L"true";
static const std::wstring json_false = L"false";
static const std::wstring json_null = L"null"; 

namespace simframework
{
	namespace json
	{
		void Value::add(double value, std::wostream& stream)
		{
			stream << value;
		}

		void Value::add(const std::wstring& value, std::wostream& stream)
		{	
			stream << json_string_delimiter << value << json_string_delimiter;
		}

		void Value::add( bool value , std::wostream& stream)
		{
			stream << (value ? json_true : json_false) ;			
		}

		void Value::add( int value , std::wostream& stream)
		{
			stream << value;
		}

		void Value::add(NullType value, std::wostream& stream)
		{
			stream << json_null;			
		}

		void Value::add( char value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( unsigned char value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( signed char value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( short value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( unsigned short value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( unsigned int value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( float value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( long value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( unsigned long value , std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( long double value, std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( long long value, std::wostream& stream )
		{
			stream << value;
		}

		void Value::add( const char* value, std::wostream& stream )
		{
			stream << json_string_delimiter << value << json_string_delimiter;
		}

		void Value::add( const unsigned char* value, std::wostream& stream )
		{
			stream << json_string_delimiter << value << json_string_delimiter;
		}

		void Value::add( const signed char* value, std::wostream& stream )
		{
			stream << json_string_delimiter << value << json_string_delimiter;
		}

		void Value::add( wchar_t value , std::wostream& stream )
		{
			stream << json_string_delimiter << value << json_string_delimiter;
		}		

		void Value::add( const wchar_t* value , std::wostream& stream )
		{
			std::wstring temp(L"");
			std::wstring str(value);

			for (int i=0; i<str.length(); ++i)
			{
				wchar_t character=str[i];
				if(character==L'\x22'){
					temp.append(json_escape_quotation);
				}else if(character==L'\x5c'){
					temp.append(json_escape_backslash);
				}else if(character==L'\x2f'){
					temp.append(json_escape_slash);
				}else if(character==L'\x08'){
					temp.append(json_escape_backspace);
				}else if(character==L'\x0c'){
					temp.append(json_escape_formfeed);
				}else if(character==L'\x0a'){
					temp.append(json_escape_newline);
				}else if(character==L'\x0d'){
					temp.append(json_escape_carriage);
				}else if(character==L'\x09'){
					temp.append(json_escape_tab);
				}else if(character>L'\u00ff'){
					temp.append(json_escape_hexdigits);
					temp.append(1,(character-(character % 256))/256);
					temp.append(1,character % 256);
				}else{
					temp.append(1, character);
				}
			}
			stream << json_string_delimiter << temp << json_string_delimiter;
		}		
	}
}